//
//  AppDelegate.m
//  cjyCalculator
//计算器能实现 1+2＊3/2-5运算
//  Created by jianyi.chen on 17/8/25.
//  Copyright © 2017年 Incube. All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@property (weak) IBOutlet NSWindow *window;
@property (nonatomic, strong) NSMutableArray *Array;   //存数字
@property (nonatomic, strong) NSMutableArray *Array2;  //存符号
@end
@implementation AppDelegate
{
    int num;                                      //判断状态：0:正常状态; 1：前一步进行了"＝"号运算；2:前一步进行了清零；3:清屏
}
-(NSMutableArray *)Array{
    if(!_Array)
        _Array = [[NSMutableArray alloc] init];
    return _Array;
}
-(NSMutableArray *)Array2{
    if(!_Array2)
        _Array2 = [[NSMutableArray alloc] init];
    return _Array2;
}
//输入数字
-(void)btn:(id)sender{
    if(num==1){
        self.textLabel.stringValue=@"";
        [_Array2 removeAllObjects];
        [_Array removeAllObjects];
        num=2;
    }else if(num==3){
        self.textLabel.stringValue=@"";
        num=0;
    }
    NSString *btn=[sender title];
    if([btn floatValue]==0){              //输入0时判断输入框里是否存在数字，存在数字时才能存入多个0，不存在时只能输入一个0
        if([self.textLabel.stringValue floatValue]>=1){
            self.textLabel.stringValue=[self.textLabel.stringValue stringByAppendingString:btn];
        }else{
            self.textLabel.stringValue=[NSString stringWithFormat:@"%@", btn];
        }
    }else{
        if([self.textLabel.stringValue floatValue]==0){     //判断显示框里是否为0，为0则先清空再输入数据
            self.textLabel.stringValue=@"";
        }
        self.textLabel.stringValue=[self.textLabel.stringValue stringByAppendingString:btn];
    }
    
    self.Ac.title=@"C";
}
//加号运算
-(void)btnAdd:(id)sender{
    NSInteger t2=_Array2.count;
    if(![self.textLabel.stringValue isEqual:@""]){
        
        if(num==2){                                                           //判断是非是清零状态
            [ self.Array  addObject:self.textLabel.stringValue ];
            [self.Array2 addObject:@"+"];
            num=3;
        }else{
            if(num==1){                                                               //判断是非处于等号状态
                num=0;
            }else{
                
                [ self.Array  addObject:self.textLabel.stringValue ];
                if([_Array2[0] isEqual:@"+"]){
                    if(t2==2){                                                        //判断Array2是否存在两个元素
                        if([_Array2[1] isEqual:@"*"]){
                            [self operateArray1:1 select:1];                    //Array[0]+Array[1]*Array[2]
                            [self printf];
                        }else{
                            [self operateArray1:2 select:1];                    //Array[0]+Array[1]/Array[2]
                            [self printf];
                        }
                    }
                    else{                                                                                                      //Array2只存在一个元素时
                        [self operateArray1:1 select:2];                          //Array[0]+Array[1]
                        [self printf];
                    }
                }else if([_Array2[0] isEqual:@"-"]){
                    if(t2==2){
                        if([_Array2[1] isEqual:@"*"]){
                            [self operateArray1:3 select:1];                         //Array[0]-Array[1]*Array[2]
                            [self printf];
                        }else{
                            [self operateArray1:4 select:1];                          //Array[0]-Array[1]/Array[2]
                            [self printf];
                        }
                    }else{
                        [self operateArray1:2 select:2];                                 //Array[0]-Array[1]
                        [self printf];
                        
                    }
                }else if([_Array2[0] isEqual:@"*"]){
                    [self operateArray1:3 select:2];                                      //Array[0]*Array[1]
                    [self printf];
                }else if([_Array2[0] isEqual:@"/"]){
                    [self operateArray1:4 select:2];                                        //Array[0]/Array[1]
                    [self printf];
                }
                
            }
            [self.Array2 addObject:@"+"];
            num=3;
        }
    }
}
//减号运算
-(void)btnRemove:(id)sender{
    NSInteger t2=_Array2.count;
    
    if(![self.textLabel.stringValue isEqual:@""]){
        
        if(num==2){
            [ self.Array  addObject:self.textLabel.stringValue ];
            [self.Array2 addObject:@"-"];
            num=3;
        }else{
            if(num==1){
                num=0;
            }else{
                [ self.Array  addObject:self.textLabel.stringValue ];
                if([_Array2[0] isEqual:@"+"]){
                    if(t2==2){
                        if([_Array2[1] isEqual:@"*"]){
                            [self operateArray1:1 select:1];
                            [self printf];
                        }else{
                            [self operateArray1:2 select:1];
                            [self printf];
                        }
                    }else{
                        [self operateArray1:1 select:2];
                        [self printf];
                    }
                }else  if([_Array2[0] isEqual:@"-"]){
                    if(t2==2){
                        if([_Array2[1] isEqual:@"*"]){
                            [self operateArray1:3 select:1];
                            [self printf];
                        }else{
                            [self operateArray1:4 select:1];
                            [self printf];
                        }
                    }else{
                        [self operateArray1:2 select:2];
                        [self printf];
                    }  }else if([_Array2[0] isEqual:@"*"]){
                        [self operateArray1:3 select:2];
                        [self printf];
                    }else  if([_Array2[0] isEqual:@"/"]){
                        [self operateArray1:4 select:2];
                        [self printf];
                    }
            }
            [self.Array2 addObject:@"-"];
            num=3;
        }
    }
    
}
//乘号运算
-(void)btnMult:(id)sender{
    
    
    if(![self.textLabel.stringValue isEqual:@""]){
        
        if(num==2){
            [ self.Array  addObject:self.textLabel.stringValue ];
            [self.Array2 addObject:@"*"];
            num=3;
        }else{
            if(num==1){
                num=0;
            }
            else{
                [ self.Array  addObject:self.textLabel.stringValue ];
                if([_Array2[0] isEqual:@"*"]){
                    [self operateArray1:3 select:2];                                       //Array[0]*Array[1]
                    [self printf];
                }else   if([_Array2[0] isEqual:@"/"]){
                    [self operateArray1:4 select:2];                                      //Array[0]/Array[1]
                    [self printf];
                }
                NSInteger t2=_Array2.count;
                if(t2==2){
                    if(([_Array2[0] isEqual:@"+"]||[_Array2[0] isEqual:@"-"])&&[_Array2[1] isEqual:@"*"])
                    {
                        [self operateArray1:1 select:3];                                              //Array[0]不变，Array[1]=Array[1]*Array[2]
                    }else if(([_Array2[0] isEqual:@"+"]||[_Array2[0] isEqual:@"-"])&&[_Array2[1] isEqual:@"/"]){
                        [self operateArray1:2 select:3];                                               //Array[0]不变，Array[1]=Array[1]/Array[2]
                    }
                }
            }
            [self.Array2 addObject:@"*"];
            num=3;
        }
    }
    
}
//除号运算
-(void)btnDivision:(id)sender{
    
    if(![self.textLabel.stringValue isEqual:@""]){
        
        if(num==2){
            [ self.Array  addObject:self.textLabel.stringValue ];
            [self.Array2 addObject:@"/"];
            num=3;
        }else{
            if(num==1){
                num=0;
            }else{
                [ self.Array  addObject:self.textLabel.stringValue ];
                if([_Array2[0] isEqual:@"*"]){
                    [self operateArray1:3 select:2];
                    [self printf];
                }else    if([_Array2[0] isEqual:@"/"]){
                    [self operateArray1:4 select:2];
                    [self printf];
                }
                NSInteger t2=_Array2.count;
                if(t2==2){
                    if(([_Array2[0] isEqual:@"+"]||[_Array2[0] isEqual:@"-"])&&[_Array2[1] isEqual:@"*"])
                    {
                        [self operateArray1:1 select:3];
                    }else if(([_Array2[0] isEqual:@"+"]||[_Array2[0] isEqual:@"-"])&&[_Array2[1] isEqual:@"/"]){
                        [self operateArray1:2 select:3];
                    }
                }
            }
            [self.Array2 addObject:@"/"];
            num=3;
        }
    }
}
//清零
-(void)btnClear:(id)sender{
    if(![self.textLabel.stringValue isEqual:@""]){
        self.textLabel.stringValue=@"";
        if(!_Array){
        }
        self.Ac.title=@"AC";
    }else{
        self.textLabel.stringValue=@"";
        [_Array2 removeAllObjects];    //清除所有存储的符号
        [_Array removeAllObjects];      //清除所有存储的数字
        num=2;                                 //清零标记
        self.Ac.title=@"AC";
    }
}
//等号
-(void)btnEqual:(id)sender{
     NSInteger t1=_Array.count;
     NSInteger t=_Array2.count;
       if(![self.textLabel.stringValue isEqual:@""]){
           if(t1>0&&t>0){
    [ self.Array  addObject:self.textLabel.stringValue ];
    self.num1=[_Array[0] floatValue];
    self.num2=[_Array[1] floatValue];
   
    if([_Array2[0] isEqual:@"+"]){
        if(t==2){                                           //Array2存在两个元素
            if([_Array2[1] isEqual:@"*"]){
                self.num4=[_Array[2] floatValue];
                self.num3=self.num1+self.num2*self.num4;
            }else if([_Array2[1] isEqual:@"/"]){
                self.num4=[_Array[2] floatValue];
                self.num3=self.num1+self.num2/self.num4;
            }
        }else{
            self.num3=self.num1+self.num2;
        }
    }
    else if([_Array2[0] isEqual:@"-"]){
        if(t==2){                                                //Array2存在两个元素
            if([_Array2[1] isEqual:@"*"]){
                self.num4=[_Array[2] floatValue];
                self.num3=self.num1-self.num2*self.num4;
            }else if([_Array2[1] isEqual:@"/"]){
                self.num4=[_Array[2] floatValue];
                self.num3=self.num1-self.num2/self.num4;
            }
        }
        else{
            self.num3=self.num1-self.num2;
        }
    }
    else if([_Array2[0] isEqual:@"*"]){
        self.num3=self.num1*self.num2;
    }else if([_Array2[0] isEqual:@"/"]){
        self.num3=self.num1/self.num2;
    }
    [_Array replaceObjectAtIndex:0 withObject:[NSString stringWithFormat: @"%.4f", self.num3]];
    [self printf];
    [_Array removeObjectAtIndex:1];
    [_Array2 removeAllObjects];
    num=1;                                                                                         //等号标记
}
       }
}
//输出
-(void)printf{
    if(self.num3 -[_Array[0] intValue] ==0){                                                     //判断运算得到的数是否为整数
        self.textLabel.stringValue=[NSString stringWithFormat:@"%d",[_Array[0] intValue]];
    }else{
        self.textLabel.stringValue=[NSString stringWithFormat:@"%.4f",self.num3]; //运算得到的数不是整数时，保留小数点后四位
    }
}
//对Array和Array2的操作
-(void)operateArray1:(int)a select:(int)b{
    if(b==1){                                                     //Array2[0]为＋或者－时，Array2[1]为＊或者／，再次点击＋或者－
        self.num1=[_Array[0] floatValue];
        self.num2=[_Array[1] floatValue];
        self.num4=[_Array[2] floatValue];
        if(a==1){
            self.num3= self.num1+self.num2*self.num4;
        }else if(a==2){
            self.num3= self.num1+self.num2/self.num4;
        }else if(a==3){
            self.num3= self.num1-self.num2*self.num4;
        }else if(a==4){
            self.num3= self.num1-self.num2/self.num4;
        }
        [_Array replaceObjectAtIndex:0 withObject:[NSString stringWithFormat: @"%f", self.num3]];
        [_Array removeObjectAtIndex:2];
        [_Array removeObjectAtIndex:1];
        [_Array2 removeObjectAtIndex:1];
        [_Array2 removeObjectAtIndex:0];
    }else  if(b==2){                                          //Array2只含有一个运算符，优先级相同运算
        self.num1=[_Array[0] floatValue];
        self.num2=[_Array[1] floatValue];
        if(a==1){
            self.num3=self.num1+self.num2;
        }
        else if(a==2){
            self.num3=self.num1-self.num2;
        }
        else if(a==3){
            self.num3=self.num1*self.num2;
        }
        else if(a==4){
            self.num3=self.num1/self.num2;
        }
        [_Array replaceObjectAtIndex:0 withObject:[NSString stringWithFormat: @"%f", self.num3]];
        [_Array removeObjectAtIndex:1];
        [_Array2 removeObjectAtIndex:0];
    }else if(b==3){                                                //Array2[0]为＋或者－时，Array2[1]为＊或者／，再次点击＊或者／
        self.num1=[_Array[0] floatValue];
        self.num2=[_Array[1] floatValue];
        self.num4=[_Array[2] floatValue];
        if(a==1){
            self.num3=  self.num2*self.num4;
        }if(a==2){
            self.num3=  self.num2/self.num4;
        }
        [_Array replaceObjectAtIndex:1 withObject:[NSString stringWithFormat: @"%f", self.num3]];
        [_Array removeObjectAtIndex:2];
        [_Array2 removeObjectAtIndex:1];
    }
}



- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
}

- (void)applicationWillTerminate:(NSNotification *)aNotification {
    
}

@end

